//
//  PopUpView.m
//  WashForU
//
//  Created by 王俊 on 15/11/30.
//  Copyright © 2015年 王俊. All rights reserved.
//

#import "PopUpView.h"
@interface PopUpView()
@property(nonatomic,strong)UIView      *contentView;
@property(nonatomic,strong)UIControl   *barrierView;
@property(nonatomic,strong)UIImageView *backgroundImage;
@property(nonatomic,strong)UIImageView *clotheImage;
@property(nonatomic,strong)UILabel     *priceLabel;
@property(nonatomic,strong)UILabel     *titleLabel;
@property(nonatomic,strong)UILabel     *moduleLabel;
@property(nonatomic,strong)UILabel     *descLabel;
@property(nonatomic,strong)UIButton    *addButton;
@property(nonatomic,strong)Counter     *counter;

@end

@implementation PopUpView


-(instancetype)initWithClotheData:(ClothesInfo*) clotheData
{
    self = [super init];
    self.data = clotheData;
    [self setUpView];
    [self addConstraint];
    return self;
    
    
}

-(void)setUpView
{
    self.contentView     = [[UIView alloc]init];
    self.barrierView     = [[UIControl alloc]init];
    self.backgroundImage = [[UIImageView alloc]init];
    self.clotheImage     = [[UIImageView alloc]init];
    self.priceLabel      = [[UILabel alloc]init];
    self.moduleLabel     = [[UILabel alloc]init];
    self.titleLabel      = [[UILabel alloc]init];
    self.addButton       = [[UIButton alloc]init];
    self.descLabel       = [[UILabel alloc]init];
    self.counter         = [[Counter alloc]initView];
    
    self.counter.numb = 1;
    self.barrierView.backgroundColor = [UIColor blackColor];
    self.barrierView.alpha = 0;
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    self.titleLabel.text = self.data.title;
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.font = [UIFont CustomFont:20];

    self.priceLabel.text = [NSString stringWithFormat:@"%d",self.data.price];
    self.priceLabel.textAlignment = NSTextAlignmentRight;
    self.priceLabel.textColor = [UIColor CustomOrangeColor];
    self.priceLabel.font = [UIFont CustomFont:23];

    
    self.moduleLabel.text = self.data.module;
    self.moduleLabel.font = [UIFont CustomFont:17];
    
    if (self.data.desc) {
        self.descLabel.text = self.data.desc;
    }
    self.descLabel.numberOfLines = 0;
    self.descLabel.textColor = [UIColor GrayColor_88];
    self.descLabel.font = [UIFont CustomFont:10];
    
    [self.addButton setImage:[UIImage imageNamed:@"确认添加按钮图"] forState:UIControlStateNormal];
    [self.addButton setImage:[UIImage imageNamed:@"确认添加按钮图H"] forState:UIControlStateHighlighted];
    [self.addButton addTarget:self action:@selector(addButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    self.backgroundImage.image = [UIImage imageNamed:self.data.backgroundImageName];
    self.clotheImage.image     = [UIImage imageNamed:self.data.imageName];
    [self.barrierView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    //    priceLabel.text       = clotheData.price;
    //    moduleLabel.text      = clotheData.module;
    //    titleLabel.text       = clotheData.title;
    [self addSubview:self.barrierView];
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.backgroundImage];
    [self.backgroundImage addSubview:self.clotheImage];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.priceLabel];
    [self.contentView addSubview:self.moduleLabel];
    [self.contentView addSubview:self.addButton];
    [self.contentView addSubview:self.descLabel];
    [self.contentView addSubview:self.counter];

}

-(void)addConstraint
{
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(SCREENWIDTH - 40);
        make.centerX.equalTo(self);
        make.top.equalTo(self.mas_bottom);
    }];
    [self.barrierView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.backgroundImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(self.contentView).offset(20);
        make.size.mas_equalTo(CGSizeMake(BACKIMAGEWIDTH, BACKIMAGEHEIGHT));
    }];
    [self.clotheImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(CLOTHEIMAGEWIDTH, CLOTHEIMAGEHEIGHT));
        make.center.equalTo(self.backgroundImage);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(70, 25));
        make.leading.equalTo(self.backgroundImage.mas_trailing).offset(20);
        make.top.equalTo(self.backgroundImage);
    }];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(70, 25));
        make.leading.equalTo(self.titleLabel);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(5);
    }];
    [self.moduleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.priceLabel.mas_trailing);
        make.size.mas_equalTo(CGSizeMake(70, 25));
        make.top.equalTo(self.priceLabel).offset(4);
    }];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).offset(4);
        make.trailing.equalTo(self.contentView).offset(-4);
        make.height.mas_equalTo(45);
        make.bottom.equalTo(self.contentView).offset(-4);
    }];
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.priceLabel);
        make.top.equalTo(self.priceLabel.mas_bottom);
        make.trailing.equalTo(self.contentView).offset(-20);
        make.height.mas_equalTo(100);
    }];
    [self.counter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(180, 45));
        make.centerX.equalTo(self.contentView);
        make.bottom.equalTo(self.addButton.mas_top).offset(-20);
    }];
}
+(instancetype)show:(ClothesInfo *)data
{
    PopUpView *popView = [[PopUpView alloc]initWithClotheData:data];
    popView.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
    [[[[UIApplication sharedApplication] delegate]window]addSubview:popView];
    [popView present];
    return popView;
    
}
-(void)present
{
    [UIView animateWithDuration:0.3 animations:^{
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_bottom).offset(- SCREENWIDTH + 20 );
            make.height.width.mas_equalTo(SCREENWIDTH - 40);
            make.centerX.equalTo(self);
        }];
        [self.contentView layoutIfNeeded];
        self.barrierView.alpha += 0.5;
    }];
    
}
-(void)dismiss
{

    [UIView animateWithDuration:0.3 animations:^{
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_bottom);
            make.height.width.mas_equalTo(SCREENWIDTH - 40);
            make.centerX.equalTo(self);
        }];
        [self.contentView layoutIfNeeded];
        self.barrierView.alpha -= 0.5;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)addButtonClicked
{
    [self addToCartWithData:self.data andNumber:self.counter.numb];
    [self dismiss];
}

-(void)addToCartWithData:(ClothesInfo*) data andNumber:(int) addNumb
{
    CartSingleton *shareSingleton = [CartSingleton shareCartSingleton];
    BOOL needAdd = true;
    if ([self arrayIsNotEmpty:shareSingleton.itermInCart]) {
        for (int i = 0 ; i < shareSingleton.itermInCart.count; i++) {
            if (data.title == shareSingleton.itermInCart[i].clotheData.title) {
                shareSingleton.itermInCart[i].numb += addNumb;
                needAdd = false;
            }
        }
    }
    if (needAdd && addNumb != 0) {
        ClotheInCart *newAdd = [[ClotheInCart alloc]initData:data andNumb:addNumb];
        [shareSingleton.itermInCart addObject:newAdd];
        NSLog(@"add a new item");
    }
    
    
}

-(BOOL)arrayIsNotEmpty:(NSArray*) array
{
    return array.count > 0 ? true :false;
}


@end
